// 单位换算配置
const unitConfigs = {
    length: {
        name: '长度',
        units: {
            m: { name: '米', factor: 1 },
            km: { name: '千米', factor: 1000 },
            cm: { name: '厘米', factor: 0.01 },
            mm: { name: '毫米', factor: 0.001 },
            ft: { name: '英尺', factor: 0.3048 },
            in: { name: '英寸', factor: 0.0254 },
            yd: { name: '码', factor: 0.9144 },
            mi: { name: '英里', factor: 1609.344 }
        }
    },
    weight: {
        name: '重量',
        units: {
            kg: { name: '千克', factor: 1 },
            g: { name: '克', factor: 0.001 },
            mg: { name: '毫克', factor: 0.000001 },
            t: { name: '吨', factor: 1000 },
            lb: { name: '磅', factor: 0.453592 },
            oz: { name: '盎司', factor: 0.0283495 }
        }
    },
    temperature: {
        name: '温度',
        units: {
            c: { name: '摄氏度', factor: 1 },
            f: { name: '华氏度', factor: 1 },
            k: { name: '开尔文', factor: 1 }
        }
    },
    volume: {
        name: '体积',
        units: {
            l: { name: '升', factor: 1 },
            ml: { name: '毫升', factor: 0.001 },
            m3: { name: '立方米', factor: 1000 },
            cm3: { name: '立方厘米', factor: 0.001 },
            gal: { name: '加仑(美)', factor: 3.78541 },
            qt: { name: '夸脱', factor: 0.946353 }
        }
    }
};

// 常用换算参考数据
const quickReference = {
    length: [
        { from: '1 英尺', to: '0.3048 米' },
        { from: '1 英寸', to: '2.54 厘米' },
        { from: '1 米', to: '3.2808 英尺' },
        { from: '1 千米', to: '0.6214 英里' }
    ],
    weight: [
        { from: '1 磅', to: '0.4536 千克' },
        { from: '1 盎司', to: '28.35 克' },
        { from: '1 千克', to: '2.2046 磅' },
        { from: '1 吨', to: '2204.62 磅' }
    ],
    temperature: [
        { from: '0°C', to: '32°F' },
        { from: '100°C', to: '212°F' },
        { from: '32°F', to: '0°C' },
        { from: '98.6°F', to: '37°C' }
    ],
    volume: [
        { from: '1 升', to: '0.2642 加仑' },
        { from: '1 加仑', to: '3.785 升' },
        { from: '1 立方米', to: '1000 升' },
        { from: '1 毫升', to: '0.0338 盎司' }
    ]
};

let currentType = 'length';

// 初始化
document.addEventListener('DOMContentLoaded', function() {
    initializeUnitSelector();
    initializeEventListeners();
    updateUnitOptions();
    convert();
});

// 初始化单位选择器
function initializeUnitSelector() {
    const buttons = document.querySelectorAll('.unit-btn');
    buttons.forEach(btn => {
        btn.addEventListener('click', function() {
            buttons.forEach(b => b.classList.remove('active'));
            this.classList.add('active');
            currentType = this.dataset.type;
            updateUnitOptions();
            convert();
        });
    });
}

// 初始化事件监听器
function initializeEventListeners() {
    document.getElementById('inputValue').addEventListener('input', convert);
    document.getElementById('inputUnit').addEventListener('change', convert);
    document.getElementById('outputUnit').addEventListener('change', convert);
}

// 更新单位选项
function updateUnitOptions() {
    const inputUnit = document.getElementById('inputUnit');
    const outputUnit = document.getElementById('outputUnit');
    const units = unitConfigs[currentType].units;
    
    inputUnit.innerHTML = '';
    outputUnit.innerHTML = '';
    
    Object.keys(units).forEach(unit => {
        const option1 = new Option(units[unit].name, unit);
        const option2 = new Option(units[unit].name, unit);
        inputUnit.add(option1);
        outputUnit.add(option2);
    });
    
    // 设置默认值
    if (currentType === 'length') {
        inputUnit.value = 'ft';
        outputUnit.value = 'm';
    } else if (currentType === 'weight') {
        inputUnit.value = 'lb';
        outputUnit.value = 'kg';
    } else if (currentType === 'temperature') {
        inputUnit.value = 'f';
        outputUnit.value = 'c';
    } else if (currentType === 'volume') {
        inputUnit.value = 'l';
        outputUnit.value = 'gal';
    }
    
    updateQuickReference();
}

// 单位转换
function convert() {
    const inputValue = parseFloat(document.getElementById('inputValue').value);
    const inputUnit = document.getElementById('inputUnit').value;
    const outputUnit = document.getElementById('outputUnit').value;
    
    if (isNaN(inputValue)) {
        document.getElementById('outputValue').value = '';
        return;
    }
    
    let result;
    
    if (currentType === 'temperature') {
        result = convertTemperature(inputValue, inputUnit, outputUnit);
    } else {
        const units = unitConfigs[currentType].units;
        const inputFactor = units[inputUnit].factor;
        const outputFactor = units[outputUnit].factor;
        result = (inputValue * inputFactor) / outputFactor;
    }
    
    document.getElementById('outputValue').value = result.toFixed(6);
}

// 温度转换
function convertTemperature(value, fromUnit, toUnit) {
    if (fromUnit === toUnit) return value;
    
    let celsius;
    
    // 转换为摄氏度
    switch (fromUnit) {
        case 'c':
            celsius = value;
            break;
        case 'f':
            celsius = (value - 32) * 5/9;
            break;
        case 'k':
            celsius = value - 273.15;
            break;
    }
    
    // 从摄氏度转换为目标单位
    switch (toUnit) {
        case 'c':
            return celsius;
        case 'f':
            return celsius * 9/5 + 32;
        case 'k':
            return celsius + 273.15;
    }
}

// 交换单位
function swapUnits() {
    const inputUnit = document.getElementById('inputUnit');
    const outputUnit = document.getElementById('outputUnit');
    const inputValue = document.getElementById('inputValue');
    const outputValue = document.getElementById('outputValue');
    
    // 交换单位
    const tempUnit = inputUnit.value;
    inputUnit.value = outputUnit.value;
    outputUnit.value = tempUnit;
    
    // 交换数值
    if (outputValue.value) {
        inputValue.value = outputValue.value;
        convert();
    }
}

// 切换参考表
function toggleReference() {
    const content = document.getElementById('referenceContent');
    const icon = document.getElementById('toggleIcon');
    
    content.classList.toggle('show');
    icon.style.transform = content.classList.contains('show') ? 'rotate(180deg)' : 'rotate(0deg)';
}

// 更新快速参考
function updateQuickReference() {
    const grid = document.getElementById('referenceGrid');
    const references = quickReference[currentType];
    
    grid.innerHTML = '';
    references.forEach(ref => {
        const item = document.createElement('div');
        item.className = 'reference-item';
        item.innerHTML = `
            <h4>${ref.from}</h4>
            <p>= ${ref.to}</p>
        `;
        grid.appendChild(item);
    });
}
